package com.fwtai.controller.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.service.web.UserAccountService;
import com.fwtai.tool.ToolClient;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 系统用户控制层|路由层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2021-07-21 17:49:27
 * @官网 <url>http://www.fwtai.com</url>
*/
@RestController
@RequestMapping("/userAccount")
public class UserAccountController{

  private final UserAccountService useraccountService;

  public UserAccountController(final UserAccountService useraccountService){
    this.useraccountService = useraccountService;
  }

  /**添加*/
  @PreAuthorize("hasAuthority('userAccount_btn_add')")
  @PostMapping("/add")
  public void add(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(useraccountService.add(request),response);
  }

  /**编辑*/
  @PreAuthorize("hasAuthority('userAccount_row_edit')")
  @PostMapping("/edit")
  public void edit(final HttpServletRequest request,final Integer category,final HttpServletResponse response){
    if(category == null){
      ToolClient.responseJson(useraccountService.edit(request),response);
    }else{
      ToolClient.responseJson(useraccountService.editCategory(request,category),response);
    }
  }

  /**根据id查询对应的数据*/
  @PreAuthorize("hasAuthority('userAccount_row_queryById')")
  @GetMapping("/queryById")
  public void queryById(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(useraccountService.queryById(new PageFormData(request)),response);
  }

  /**控制启禁用*/
  @PreAuthorize("hasAuthority('userAccount_row_editEnabled')")
  @PostMapping("/editEnabled")
  public void editEnabled(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(useraccountService.editEnabled(ToolClient.getFormData(request)),response);
  }

  /**获取数据*/
  @PreAuthorize("hasAuthority('userAccount_btn_listData')")
  @GetMapping("/listData")
  public void listData(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(useraccountService.listData(new PageFormData(request)),response);
  }

  @PreAuthorize("hasAuthority('userAccount_row_queryLogin')")
  @PostMapping("/queryLogin")
  public void queryLogin(final Long userId,final HttpServletResponse response){
    ToolClient.responseJson(useraccountService.queryLogin(userId),response);
  }
}